﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using System.Data.SqlClient;
using PrimaryKeyFunction;

public partial class Admin_OilComponents : System.Web.UI.Page
{
    Service objService = new Service();

    Functions objFunction = new Functions();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillOilComponents(Convert.ToInt32(id));
                txtOilComponentsId.Text = string.Empty;
                trDgOilComponents.Visible = false ;
                trtxtboxOilComponents.Visible = true;
            }


            BindDataGrid();
        }
    }

    private void BindDataGrid()
    {
         string ExpID = (string)Session["ExperimentId"];
         if (ExpID != null)
         {
             if (objService.GetOilComponents(ExpID).Tables[0].Rows.Count > 0)
             {
                 dgOilComponents.DataSource = objService.GetOilComponents(ExpID);
                 dgOilComponents.DataBind();
                 trDgOilComponents.Visible = true;
                 trtxtboxOilComponents.Visible = false;
             }
             else
             {
                 trDgOilComponents.Visible = false;
                 trtxtboxOilComponents.Visible = true;
             }

         }
         else
         {
             trDgOilComponents.Visible = false;
             trtxtboxOilComponents.Visible = true;
         }

    }



    private void BindDataOilCompMisc()
    {
        string ExpID = (string)Session["ExperimentId"];
        if (ExpID == string.Empty)
        {
            trOilcompMiscDg.Visible = false;
            trOilcompMiscTextboxes.Visible = true;
        }
        else
        {
            if (objFunction.GetOilCompoMisc(ExpID).Tables[0].Rows.Count > 0)
            {
                dgOilCompMisc.DataSource = objFunction.GetOilCompoMisc(ExpID);
                dgOilCompMisc.DataBind();
                trOilcompMiscDg.Visible = true;
                trOilcompMiscTextboxes.Visible = false;
            }
            else
            {
                trOilcompMiscDg.Visible = false ;
                trOilcompMiscTextboxes.Visible = true;
            }

        }


    }
    protected void chkOilmisscellaneous_CheckedChanged(object sender, EventArgs e)
    {
        if (chkOilmisscellaneous.Checked == true)
        {
            trmisscellaneous.Visible = true;
            BindDataOilCompMisc();
        }
        else
        {
            trmisscellaneous.Visible = false;
        }
    }

    protected void btnAddOilmisscellaneous_Click(object sender, EventArgs e)
    {
        if (txtOilCompMiscId.Text == string.Empty)
        {
            string ExpID = (string)Session["ExperimentId"];

            lblMsg.Text = objService.SaveOilComponentsMisc(txtCategory.Text, txtAmount.Text, ExpID);

            string OilComponentMiscId = objFunction.GetOilCompMiscPK(ExpID);

            string OilComponentId = (string)Session["OilComponentId"];

            lblMsg.Text = objService.UpdateTable("oilcomponents", "misc_fk", OilComponentMiscId, OilComponentId);

            txtCategory.Text = string.Empty;
            txtAmount.Text = string.Empty;

            BindDataOilCompMisc();
        }
        else
        {
            if (txtCategory.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilcomps_misc", "category", "null", txtOilCompMiscId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilcomps_misc", "category", txtCategory.Text ,txtOilCompMiscId.Text);
            }

            if (txtAmount.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilcomps_misc", "amount", "null", txtOilCompMiscId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilcomps_misc", "amount", txtAmount.Text, txtOilCompMiscId.Text);
            }

            BindDataOilCompMisc();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    protected void dgOilComponents_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgOilComponents.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillOilComponents(id);
        trDgOilComponents.Visible = false;
        trtxtboxOilComponents.Visible = true;
        btnPrevious.Visible = false;
    }

    private void fillOilComponents(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("oilcomponents", id);

        txtAliphatic.Text = ds.Tables[0].Rows[0]["aliphatic"].ToString();
        txtAromatic.Text = ds.Tables[0].Rows[0]["aromatic"].ToString();
        txtPolar.Text = ds.Tables[0].Rows[0]["polar"].ToString();
        txtOilComponentsId.Text = ds.Tables[0].Rows[0]["id"].ToString();

    }

    private void fillOilComponentsMisc(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("oilcomps_misc", id);

        txtOilCompMiscId .Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtCategory.Text = ds.Tables[0].Rows[0]["category"].ToString();
        txtAmount.Text = ds.Tables[0].Rows[0]["amount"].ToString();
       
    }

    private void ClearTextBoxes()
    {
        txtAliphatic.Text = string.Empty;
        txtAromatic.Text = string.Empty;
        txtPolar.Text = string.Empty;
        txtOilComponentsId.Text = string.Empty;
        txtCategory.Text = string.Empty;
        txtAmount.Text = string.Empty;

    }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (txtOilComponentsId.Text != string.Empty)
        {
            if (txtAliphatic.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "aliphatic", "null", txtOilComponentsId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "aliphatic", txtAliphatic.Text, txtOilComponentsId.Text);
            }

            if (txtAromatic.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "aromatic", "null", txtOilComponentsId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "aromatic", txtAromatic.Text, txtOilComponentsId.Text);
            }
            if (txtPolar.Text == string.Empty)
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "polar", "null", txtOilComponentsId.Text);
            }
            else
            {
                lblMsg.Text = objService.UpdateTable("oilcomponents", "polar", txtPolar.Text, txtOilComponentsId.Text);
            }

            ClearTextBoxes();
            BindDataGrid();

        }
        else
        {
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpID = (string)Session["ExperimentId"];

                lblMsg.Text = objService.SaveOilComponents(txtAliphatic.Text, txtAromatic.Text, txtPolar.Text, "", ExpID);

                ClearTextBoxes();

                BindDataGrid();
            }
            else
            {

                string RefeId = (string)Session["ReferenceId"];

                objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);
                //string ExperimentId = objService.GetLastInsertId().ToString();

                lblMsg.Text = objService.SaveOilComponents(txtAliphatic.Text, txtAromatic.Text, txtPolar.Text, "", ExperimentId);

                Session["ExperimentId"] = ExperimentId;

                ClearTextBoxes();

                BindDataGrid();
            }



            string OilComponentId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "oilcomponents");

            Session["OilComponentId"] = OilComponentId;           

            if ((string)Session["LiquidCompId"] != null)
            {
                string LiquidCompId = (string)Session["LiquidCompId"];
                lblMsg.Text = "Oil components Saved and " + objService.UpdateTable("liquidcomp", "oilcomp_fk", OilComponentId, LiquidCompId);
            }
            else
            {
                lblMsg.Text = "Oil components Saved and " + objService.SaveLiquidComp(null, null, null, null, null, OilComponentId, (string)Session["ExperimentId"]);
                string LiquidCompId = objService.GetLiquidCompId("oilcomp_fk", OilComponentId);
                Session["LiquidCompId"] = LiquidCompId;
            }
        }
    }
    protected void dgOilComponents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgOilComponents.PageIndex = e.NewPageIndex;
        BindDataGrid();
    }
    protected void dgOilCompMisc_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgOilCompMisc.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillOilComponentsMisc(id);
        trOilcompMiscDg.Visible = false;
        trOilcompMiscTextboxes.Visible = true;

    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreOilComponents.aspx");
    }
}